class Node {
public:
    vector<Node*> children;
    bool isWord;

    Node() {
        children = vector<Node*>(26);
        isWord = false;
    }
};

class WordDictionary {
private:
    Node* root;
public:
    WordDictionary() {
        this->root = new Node();
    }

    void addWord(string word) {
        Node* curr = root;
        for (char c : word) {
            if (curr->children[c - 'a'] == nullptr) {
                curr->children[c - 'a'] = new Node();
            }
            curr = curr->children[c - 'a'];
        }
        curr->isWord = true;
    }

    bool search(string word) {
        return dfs(root, word, 0);
    }

    bool dfs(Node* node, string word, int index) {
        if (index == word.size()) return node->isWord;
        char c = word[index];
        if (c != '.') {
            if (node->children[c - 'a'] == nullptr) return false;
            return dfs(node->children[c - 'a'], word, index + 1);
        } else {
            for (Node* child : node->children) {
                if (child != nullptr && dfs(child, word, index + 1)) return true;
            }
            return false;
        }
    }
};

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary* obj = new WordDictionary();
 * obj->addWord(word);
 * bool param_2 = obj->search(word);
 */